package com.qingneng.web;

import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.qingneng.service.sdk.IProjectInfoService;
import com.qingneng.service.sdk.IWorkBudgetService;
import com.qingneng.vo.BudgetFeeDoInfo;
import com.qingneng.vo.ProjectInfo;
import com.qingneng.vo.ProjectWorkInfo;
import com.tbfs.common.BaseAction;
import com.tbfs.util.UtilTool;

public class SaveFeeItemDoAction extends BaseAction {
	
	private static final Logger logger  = Logger.getLogger(SaveFeeItemDoAction.class);
	
	public IWorkBudgetService workBudgetService;
	
	
	public String rowlist  =  null;
	
	public int project_workinfo_id;
	
	public int project_id;
	
	public int getProject_id() {
		return project_id;
	}
	public void setProject_id(int project_id) {
		this.project_id = project_id;
	}
	public int getProject_workinfo_id() {
		return project_workinfo_id;
	}
	public void setProject_workinfo_id(int project_workinfo_id) {
		this.project_workinfo_id = project_workinfo_id;
	}
	public String getRowlist() {
		return rowlist;
	}
	public void setRowlist(String rowlist) {
		this.rowlist = rowlist;
	}
	public SaveFeeItemDoAction(){
		
	}
	public String message;
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	private IProjectInfoService projectInfoService;
	
	public SaveFeeItemDoAction(IWorkBudgetService workBudgetService,IProjectInfoService projectInfoService){
		this.workBudgetService = workBudgetService;
		this.projectInfoService = projectInfoService;
	}
	
	
	private double workInfo_budget_totalAmount;
	
	public String execute(){
		if(logger.isDebugEnabled()){
			logger.debug("+++begin to execute SaveFeeItemDoAction +++ ");
		}
		logger.info("rowlist:"+rowlist);
		logger.info("project_workinfo_id:"+project_workinfo_id);
		if(rowlist!=null && !"".equals(rowlist)){
			
			//保存前先删除之前的取费结果，根据project_workinfo_id 删除
			BudgetFeeDoInfo  tmp  = new BudgetFeeDoInfo();
			tmp.setProject_workinfo_id(project_workinfo_id);
			logger.info("+++delete BudgetFeeDoInfo result before +++++");
			workBudgetService.destroyBudgetFeeDoInfo(tmp);
			
			//开始新增取费结果
			logger.info(" +++ add new BudgetFeeDoInfo result now ++++");
			String[] rows = rowlist.split("\\|");
			if(rows!=null && rows.length>0){
				for(int i=0;i<rows.length;i++){
					String budgetInfo_jsonString = rows[i];
					logger.info("BudgetFeeDoInfo_jsonString:"+budgetInfo_jsonString);
					BudgetFeeDoInfo  bean =  (BudgetFeeDoInfo)UtilTool.parseJsonStr2Obj(budgetInfo_jsonString,BudgetFeeDoInfo.class);
					//JSONObject jo = JSONObject.fromObject(budgetInfo_jsonString);// 格式化成json对象
					//说明为合计项目
					if(bean.getItem_id()==7 && "budget_amount".equals(bean.getFee_item_name())){
						workInfo_budget_totalAmount = bean.getFee_amount();
					}
					bean.setProject_workinfo_id(project_workinfo_id);
					workBudgetService.addBudgetFeeDoInfo(bean);
				}
			}
			//将预算合计金额更新到project_workinfo表中
			logger.info(" +++ update budgetAmount into project_workinfo table  ++++");
			ProjectWorkInfo bean = new ProjectWorkInfo();
			bean.setId(project_workinfo_id);
			bean.setCesspoolBudgetPrice(String.valueOf(workInfo_budget_totalAmount));
			//已编制
			bean.setWorkInfoStatus(1);
			try {
				workBudgetService.modifyFeeAmount2WorkInfo(bean);
				message = "true";
			} catch (Exception e) {
				message ="false";
				e.printStackTrace();
			}
			
			//将预算合计金额更新到projectinfo表中
			//取项目下所有工程的预算金额
		//	int project_id = 62;
			try {
				double projectBudgetPrice =0d;
				int projectStatus=0;
				ProjectInfo projectInfo = new ProjectInfo();
				boolean flag = true;
				List projectWorkInfo = projectInfoService.getProjectWorkInfo(project_id);
				if(projectWorkInfo!=null && projectWorkInfo.size()>0){				
					for(int j =0;j<projectWorkInfo.size();j++){
						ProjectWorkInfo record = (ProjectWorkInfo)projectWorkInfo.get(j);
						String cesspoolBudgetPrice = record.getCesspoolBudgetPrice();
						projectBudgetPrice = projectBudgetPrice+UtilTool.string2doubleFormat(cesspoolBudgetPrice);
						if(record.getWorkInfoStatus()==0){
							flag  = false;
						}
					}
				}
				projectInfo.setBudgetPrice(UtilTool.decimalFormat(projectBudgetPrice));
				projectInfo.setId(project_id);
				//编制完成
				if(flag){
					projectStatus = 1;
				}
				projectInfo.setProjectStatus(projectStatus);		
				projectInfoService.modifyProjectInfo(projectInfo);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
			
		if(logger.isDebugEnabled()){
			logger.debug("+++end to execute SaveFeeItemDoAction +++ ");
		}
		return Action.SUCCESS;
	}

}
